package guoanmaker.provider.business.controller;

import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import javax.transaction.Transactional;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import guoanmaker.provider.business.model.ProvideCommodity;
import guoanmaker.provider.business.service.ProvideCommodityServiceInterface;
import guoanmaker.provider.helper.common.AjaxJson;
import guoanmaker.provider.helper.common.abstractobj.ApiController;
import io.swagger.annotations.ApiOperation;

@RestController
@RequestMapping(ApiController.COMMODITY_URL)
public class ProvideCommodityController {

	
	@Autowired
    public ProvideCommodityController(ProvideCommodityServiceInterface commodityService) {
        this.commodityService = commodityService;
    }
	
	@Autowired
	private ProvideCommodityServiceInterface commodityService;
	
	
	@ApiOperation(value="添加商品接口", notes="添加商品信息")
	@PostMapping("/addProvideCommodity")
	@Transactional
	public  AjaxJson addProvideCommodity(@RequestBody ProvideCommodity commodity){
		AjaxJson ajaxJson=new AjaxJson();
		Integer result=commodityService.addProvideCommodity(commodity);
		ajaxJson.setSuccess(true).setStatus(result).setData(commodity);
		return ajaxJson;
	} 
	
	
	@ApiOperation(value="修改商品接口", notes="修改商品信息")
	@PostMapping("/updateProvideCommodity")
	@Transactional
	public  AjaxJson updateProvideCommodity(@RequestBody ProvideCommodity commodity){
		AjaxJson ajaxJson=new AjaxJson();
		Integer result=commodityService.updateProvideCommodity(commodity);
		ajaxJson.setSuccess(true).setStatus(result).setData(commodity);
		return ajaxJson;
	} 
	
	
	@ApiOperation(value="商品列表分页查询", notes="通过商品状态分页查询")
	@RequestMapping(value = "/findCommdity", method = RequestMethod.POST)
	//@Transactional
	public AjaxJson findCommdity( Integer page, Integer size,Integer commodityState,String businessID,String commodityName,String service_type_id
			  )throws Exception  {
		AjaxJson ajaxJson=new AjaxJson();
		Map<String, Object> reMap=commodityService.selectByCommditys(page, size,commodityState, businessID,commodityName,service_type_id);
		ajaxJson.setSuccess(true).setStatus(200).setData(reMap);
		return ajaxJson;

	}
	
	
	 @ApiOperation(value="刪除該商品", notes="刪除該商品")
	 @RequestMapping(value = "/delete", method = RequestMethod.POST)
	 @Transactional
	 public AjaxJson delete(String id){
		 AjaxJson ajaxJson=new AjaxJson();
		try {
			 commodityService.deleteCommdity(id);
		} catch (Exception e) {
			return ajaxJson.setSuccess(true).setStatus(250).setData("数据删除失败！");
		}
	    return ajaxJson.setSuccess(true).setStatus(200).setData("数据删除成功！");
	 }
	 
	 @ApiOperation(value="查询商品信息", notes="通过商品主键查询商品")
	 @RequestMapping(value = "/getProvideCommodity", method = RequestMethod.POST)
	 //@Transactional
	 public AjaxJson getProvideCommodity(String id)throws Exception  {
		 AjaxJson ajaxJson=new AjaxJson();
			 ProvideCommodity commodity =commodityService.selectById(id);
			 ajaxJson.setSuccess(true).setStatus(200).setData(commodity);
			return ajaxJson;
	}
	 
	 @ApiOperation(value="批量删除商品", notes="批量删除商品")
	 @RequestMapping(value = "/deleteIds", method = RequestMethod.POST)
	 @Transactional
	 public AjaxJson deleteIds(String ids){
		 AjaxJson ajaxJson=new AjaxJson();
		 try {
			 commodityService.deleteCommditys(ids);
		} catch (Exception e) {
			return ajaxJson.setSuccess(true).setStatus(250).setData("数据删除失败！");
		}
	    return ajaxJson.setSuccess(true).setStatus(200).setData("数据删除成功！");
	 }
	 
	 @ApiOperation(value="修改商品库存信息", notes="修改商品库存信息")
	 @RequestMapping(value = "/updateCommditysStock", method = RequestMethod.POST)
	 @Transactional
	 public AjaxJson updateCommditysStock(String id,Integer stock){
		 AjaxJson ajaxJson=new AjaxJson();
		  boolean bool=commodityService.updateCommditysStock(id, stock);
		  if(bool==true){
			  return ajaxJson.setSuccess(true).setStatus(200).setData("数据修改成功！");
		  }else{
			  return ajaxJson.setSuccess(true).setStatus(250).setData("数据修改失败！");
		  }
	 }
	 
	 @ApiOperation(value="修改商品的商品状态", notes="修改商品的商品状态")
	 @RequestMapping(value = "/updateCommditysState", method = RequestMethod.POST)
	 @Transactional
	 public AjaxJson updateCommditysState(String ids,Integer state){
		 AjaxJson ajaxJson=new AjaxJson();
		  boolean bool=commodityService.updateCommditysState(ids, state);
		  if(bool==true){
			  return ajaxJson.setSuccess(true).setStatus(200).setData("数据操作成功！");
		  }else{
			  return ajaxJson.setSuccess(true).setStatus(250).setData("数据操作失败！");
		  }
	 }
	 
	 
	 @ApiOperation(value="商品上架", notes="修改商品的商品状态")
	 @RequestMapping(value = "/updateCommditysShelvesState", method = RequestMethod.POST)
	 @Transactional
	 public AjaxJson updateCommditysShelvesState(String ids,Integer state){
		 AjaxJson ajaxJson=new AjaxJson();
		  Integer result=commodityService.updateCommditysShelvesState(ids, state);
		  return ajaxJson.setSuccess(true).setStatus(result).setData("数据操作成功！");
	 }
	 
	 @ApiOperation(value="修改商品信息", notes="修改商品信息")
	 @RequestMapping(value = "/updateCommditys", method = RequestMethod.POST)
	 @Transactional
	 public AjaxJson updateCommditys(@RequestBody ProvideCommodity commodity){
		 AjaxJson ajaxJson=new AjaxJson();
		 ProvideCommodity comm=null;
		 try {
			// comm=commodityService.updateProvideCommodity(commodity);
		} catch (Exception e) {
			return ajaxJson.setSuccess(true).setStatus(250).setData("数据操作失败！");
		}
		return ajaxJson.setSuccess(true).setStatus(200).setData(comm);
	 }
	 
}
